package com.education.api.open;

import com.education.business.utils.CertUtil;
import com.education.model.entity.CourseInfo;
import com.education.model.entity.OrgInfoEntity;
import com.education.model.entity.StudentInfo;
import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.html2pdf.resolver.font.DefaultFontProvider;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.font.FontProvider;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

@Slf4j
@RestController
@RequestMapping("/open/test")
@RequiredArgsConstructor
public class TestController {

    // 测试证书构建
    @RequestMapping("/generateCert")
    public String test() throws IOException {
        CourseInfo courseInfo = new CourseInfo();
        courseInfo.setName("语文");
        StudentInfo studentInfo = new StudentInfo();
        studentInfo.setName("张三");
        studentInfo.setId(1001);
        OrgInfoEntity orgInfoEntity = new OrgInfoEntity();
        orgInfoEntity.setOrgName("教育集团");
        String certHtmlUrl = CertUtil.generateCert("语文", studentInfo, "教育集团", "优秀");
        return certHtmlUrl;
    }

//    public static void main(String[] args) {
//        String htmlFilePath = "D:\\home\\cer\\No.cddde2d4b90a49e19683adfab96bc42f\\zhangsanyuwen.html";
//        String pdfFilePath = "D:\\home\\cer\\No.cddde2d4b90a49e19683adfab96bc42f\\zhangsanyuwen.pdf";
//
//        try (InputStream htmlStream = new FileInputStream(htmlFilePath);
//             OutputStream pdfStream = new FileOutputStream(pdfFilePath)) {
//            // 创建 PdfWriter 实例
//            PdfWriter writer = new PdfWriter(pdfStream);
//
//            // 创建 PdfDocument 并设置自定义页面大小（例如宽 1300，高 900）
//            PdfDocument pdfDoc = new PdfDocument(writer);
//            pdfDoc.setDefaultPageSize(new PageSize(1050, 750));
//
//            // 1. 使用系统已安装字体（最简单可靠的方式）
//            ConverterProperties props = new ConverterProperties();
//            DefaultFontProvider fontProvider = new DefaultFontProvider();
//            fontProvider.addSystemFonts();  // 自动加载所有系统字体
//
//            props.setFontProvider(fontProvider);
//
//            // 2. 执行转换
//            HtmlConverter.convertToPdf(htmlStream, pdfDoc, props);
//
//            System.out.println("PDF生成成功！");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

}
